import payment from "@/static/js/enterpriseServer/payment.js"
export default {
	data() {
		return {
			orderNo: '',
			orderInfo: {},
			pageShow: false,
			couponList: [], //代金券列表
			couponSelected: null, //代金券对象
			finalMoney: null, //实付总额
			payList: [
				{ name: '微信支付', id: 2, icon: 'iconzhifuye_weixinzhifu', iconColor: '#1fbe2a' },
				{ name: '支付宝支付', id: 1, icon: 'iconzhifubaozhifu', iconColor: '#01a9f2' }
			],
			payCurrent: 2,
		}
	},
	onLoad(e) {
		this.orderNo = e.orderNo
		this.getDetail()
	},
	onShow() {
		let that = this
		uni.$on('returnData', function (data) {
			if (data != null) {
				that.couponSelected = data;
			} else {
				that.couponSelected = null
			}
		})
	},
	watch: {
		"couponSelected"(newVal, oldVal) {
			if (newVal != null) {
				this.finalMoney = this.orderInfo.money - newVal.voucher_reduce
			}
		}
	},
	methods: {
		//切换支付方式
		changePay(item) {
			if (this.payCurrent != item.id) {
				this.payCurrent = item.id
			}
		},
		//获取订单详情
		async getDetail() {
			let res = await payment.orderDetail({ order_no: this.orderNo })
			if (res.code === 1) {
				this.orderInfo = res.data
				this.pageShow = true
				this.getCouponList()
			}
		},

		//立即支付
		async addBuy() {
			let that = this
			let obj = {
				order_no: this.orderNo,
				source: 'phone',
				pay_type: this.payCurrent,
			}
			if (this.couponSelected != null) {
				this.$set(obj, "voucher_id", this.couponSelected.id)
			}
			let res = await payment.payShop(obj)
			if (res.code === 1) {
				uni.requestPayment({
					provider: this.payCurrent == 1 ? "alipay" : "wxpay",
					orderInfo: res.data, //微信、支付宝订单数据
					success: (res) => {
						uni.redirectTo({
							url: '/enterpriseServer/orderList/index?index=0',
						})
					},
					fail: (res) => {
						that.couponList = []
						that.couponSelected.id = ''
					}
				});
			} else {
				this.$common.msgShow(res.msg)
			}
		},

		//获取代金券列表
		async getCouponList() {
			let res = await payment.getCouponList({
				status: 1,
				amount: this.orderInfo.money
			})
			if (res.code == 1) {
				console.log('代金券列表', res);
				this.couponList = res.data
			} else {
				this.$used.msg(res.msg)
			}
		},

		//去选择代金券
		goChoose() {
			if (this.couponSelected != null) { //携带已选择的代金券id
				uni.navigateTo({
					url: '/enterpriseServer/payment/chooseTicket/index?couponId=' + this.couponSelected.id + '&amount=' + this.orderInfo.money
				})
			} else {
				uni.navigateTo({
					url: '/enterpriseServer/payment/chooseTicket/index?amount=' + this.orderInfo.money
				})
			}

		},
	}
}